﻿// ********************************************************************
//  文件名: ReuseAddr.cs
//  描述:
//  作者: 胡宇辰
//  创建时间: 2024-4-18
// 
//  修改历史:
// ********************************************************************

using System.Net.Sockets;

namespace MyApp;

public class ReuseAddr
{
    static Socket socket;

    public static void Enter()
    {
        //客户端利用socket连接服务器
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect("127.0.0.1", 6324);
        Console.WriteLine("连接成功");
        //接收服务器返回的数据
        while (true)
        {
            byte[] buffer = new byte[1024];
            int length = socket.Receive(buffer, SocketFlags.None);
            if (length > 0)
            {
                Console.WriteLine($"{System.Text.Encoding.UTF8.GetString(buffer, 0, length)}");
            }
            else
            {
                Console.WriteLine("退出");
                socket.Close();
                break;
            }
        }
        Thread.Sleep(7000);
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect("127.0.0.1", 6324);
        Console.WriteLine("连接成功2");
    }
}